package com.song.controller.topic;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.common.Result;
import com.song.controller.BaseController;
import com.song.entity.topic.Topic;
import com.song.service.topic.TopicService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("${api.version}/topic")
public class TopicController extends BaseController {

    @Autowired
    private TopicService topicService;

    /**
     *  主题管理界面
     */
    @RequestMapping("/topicManage")
    public String topicManage() {
        return "topic/topicManage";
    }

    /**
     * 主题列表
     */
    @RequestMapping("/topics")
    public String topics() {
        return "topic/topics";
    }

    /**
     * 获取主题列表
     */
    @RequestMapping("/topicList")
    @ResponseBody
    public Result<List<Topic>> topicList(Topic topic, String startTime, String endTime,
        @RequestParam(value = "page", defaultValue = "0") int pageNum,
        @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Result<List<Topic>> result  =  topicService.selectTopicList(topic,startTime,endTime,pageNum,pageSize);
        return result;
    }

    /**
     *  根据ID删除主题
     */
    @RequestMapping("/deleteTopicById")
    @ResponseBody
    public Result deleteTopicById(int topicId) {
         boolean bool = topicService.deleteById(topicId);
         if (bool) {
            return success("删除主题成功！");
         } else {
             return error("删除主题失败！");
         }
    }

    /**
     * 批量删除主题
     */
    @RequestMapping("/batchDel")
    @ResponseBody
    public Result<Integer> batchDel(String ids) {
        String[] array = ids.split(",");
        boolean bool =  topicService.deleteBatchIds(Arrays.asList(array));
        if (bool) {
            return success("批量删除主题成功！");
        } else {
            return error("批量删除主题失败！");
        }
    }

    /**
     * 新增主题
     */
    @RequestMapping("/addTopic")
    @ResponseBody
    public Result<Integer> addTopic(Topic topic) {
        topic.setCreateTime(new Timestamp(System.currentTimeMillis()));
        topic.setCreateAdmin("admin");
        boolean bool = topicService.insert(topic);
        if (bool) {
            return success("新增主题成功！");
        } else {
            return error("新增主题失败！");
        }
    }

    /**
     * 更新主题
     */
    @RequestMapping("/updateTopic")
    @ResponseBody
    public Result<Integer> updateTopic(Topic topic) {
        boolean bool = topicService.updateById(topic);
        if (bool) {
            return success("更新主题成功！");
        } else {
            return error("更新主题失败！");
        }
    }

    /**
     * 分页查询主题列表
     */
    @RequestMapping("/selectTopics")
    @ResponseBody
    public String selectTopics(
            @RequestParam(value = "page", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        EntityWrapper<Topic> ew = new EntityWrapper<>();
        ew.eq("status",0);
        ew.orderBy("create_time");

        List<Topic> list = topicService.selectList(ew);
        PageInfo<Topic> pageInfo = new PageInfo<>(list);

        JSONObject json = new JSONObject();
        json.put("page",pageNum);
        json.put("list",list);
        json.put("pages", pageInfo.getPages());
        json.put("pageSize",pageSize);

        return json.toString();
    }
}
